﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using StockFinder.DataAccess;
using StockFinder.Entities;

namespace StockFinder.Controllers
{
    public class SymbolController
    {
        public List<Symbol> GetAllSymbols()
        {
            SymbolDataAccess symbolDataAccess = new SymbolDataAccess();

            return symbolDataAccess.GetAllSymbols();
        }

        public List<Symbol> GetAllSymbolsAndPrices(DateTime dateFrom)
        {
            List<Symbol> symbols = GetAllSymbols();

            DailyPriceDataAccess dailyPricesDataAccess = new DailyPriceDataAccess();

            foreach (Symbol symbol in symbols)
            {
                //retrieve daily prices
                dailyPricesDataAccess.GetAllDailyPricesBySymbolIdFromDate(symbol, dateFrom);

                //generate weekly prices
                GenerateWeeklyPrices(symbol);
            }

            return symbols;
        }

        /// <summary>
        /// Generates the weekly price data based on daily prices list
        /// </summary>
        /// <param name="symbol"></param>
        public void GenerateWeeklyPrices(Symbol symbol)
        {
            //make sure we have some prices
            if (symbol.Prices != null &&
                symbol.Prices.Count > 0)
            {
                symbol.WeeklyPrices.Clear();

                //loop the daily prices
                foreach (DailyPrice dailyPrice in symbol.Prices)
                {
                    //duff prices?
                    if (dailyPrice.Open == 0)
                    {
                        //yes so quit
                        break;
                    }

                    //get the week of year
                    int weekOfYear = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                        dailyPrice.PriceDate,
                        CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
                        CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);

                    //does this week/year combination already exist?
                    WeeklyPrice weeklyPrice = symbol.GetExistingWeeklyPrice(weekOfYear, dailyPrice.PriceDate.Year);

                    //does it exist?
                    if (weeklyPrice == null)
                    {
                        //no so create and setup
                        weeklyPrice = new WeeklyPrice();
                        weeklyPrice.Year = dailyPrice.PriceDate.Year;
                        weeklyPrice.WeekOfYear = weekOfYear;

                        //add to list
                        symbol.WeeklyPrices.Add(weeklyPrice);
                    }

                    //set day of week
                    switch (dailyPrice.PriceDate.DayOfWeek)
                    {
                        case DayOfWeek.Monday:

                            weeklyPrice.Monday = dailyPrice;

                            break;

                        case DayOfWeek.Tuesday:

                            weeklyPrice.Tuesday = dailyPrice;

                            break;

                        case DayOfWeek.Wednesday:

                            weeklyPrice.Wednesday = dailyPrice;

                            break;

                        case DayOfWeek.Thursday:

                            weeklyPrice.Thursday = dailyPrice;

                            break;

                        case DayOfWeek.Friday:

                            weeklyPrice.Friday = dailyPrice;

                            break;

                        default:
                            //do nothing
                            break;
                    }
                    
                }
            }
        }
    }
}
